import {
  $await
} from '../../utils/util';
const app = getApp();
import {
  selectCitytAPI
} from '../../api/public/index'
Page({
  data: {
    isPlaceholder: false,
    isSearchB: true,
    hidden: true,
    currentCity: '北京市',
    city: '',
    pop_data: [],
    area_data: [],
    searchCity: [],
    aeea_arr2: []
  },
  onLoad: function (option) {
    let latelyLocation = wx.getStorageSync('latelyLocation') || [],
      currentCity = wx.getStorageSync('area') || "全国";
    this.setData({
      currentCity,
      latelyLocation
    })
    this.getData()
  },
  async getData() {
    let [err, res] = await $await(selectCitytAPI.areaList({}))
    if (res) {
      let area_index = [],
        aeea_arr = [],
        aeea_arr1 = [],
        area_data = res.area_data,
        aeeaArr = [];
      area_index = Object.keys(area_data).sort();
      area_index.map((index) => {
        for (const key in area_data) {
          if (index == key) {
            let str = {
              'area_index': key,
              'area_data': area_data[key]
            }
            aeeaArr.push(str)
            aeea_arr.push(area_data[key])
          }
		  console.log(aeeaArr)
        }
      })
      res.pop_data.map((item)=>{
        if(item.name.length > 7){
          item.name = item.name.slice(0,3) + '...' + item.name.slice(-4)
        }
      })
      aeea_arr1 = [].concat(...aeea_arr);
      this.setData({
        area_index,
        aeea_arr1,
        pop_data: res.pop_data,
        aeeaArr
      })
    }
  },
  bindMasker() {
    this.setData({
      isSearchB: true,
      isPlaceholder: false,
      searchValue: '',
      aeea_arr2: []
    })
  },
  cityName(e) {
    let latelyLocation = wx.getStorageSync('latelyLocation') || []
    let item = latelyLocation.some((some) => {
      return some == e.currentTarget.dataset.name
    })
    console.log(item)
    if (!item && this.data.currentCity != e.currentTarget.dataset.name) {
      let latelyLocationName = e.currentTarget.dataset.name
      if(latelyLocationName.length > 7){
        latelyLocationName = latelyLocationName.slice(0,3) + '...' + latelyLocationName.slice(-4)
      }
      latelyLocation.length >= 2 ? (latelyLocation.shift() && latelyLocation.push(latelyLocationName)) : latelyLocation.push(latelyLocationName)
      wx.setStorageSync('latelyLocation', latelyLocation)
    }
    wx.reLaunch({
      url: '/pages/index/index?locationCity=' + e.currentTarget.dataset.name
    })
  },
  inputFocus() {
    this.setData({
      isPlaceholder: true
    })
  },
  inputBlur() {
    console.log(this.data.searchValue)
    if (this.data.searchValue) {
      this.setData({
        isPlaceholder: true,
      })
    } else {
      this.setData({
        isPlaceholder: false,
        aeea_arr2 : []
      })
    }
  },
  searchValue(e) {
    console.log(e.detail.value)
    this.setData({
      searchValue: e.detail.value
    })
    if (e.detail.value) {
      let aeea_arr2 = this.data.aeea_arr1.filter(array => array.name.match(e.detail.value));
      if(aeea_arr2.length > 0){
        this.setData({
          aeea_arr2,
          SearchListFalse:false
        })
      }else{
        this.setData({
          aeea_arr2:[]
        })
      }
     
      console.log(aeea_arr2)
      this.setData({
        isSearchB: false
      })
    } else {
      this.setData({
        isSearchB: true
      })
    }
  },

  //获取文字信息
  getPy: function (e) {
    if (!this.data.isSearchB) {
      return;
    }
    this.setData({
      hidden: false,
      showPy: e.target.id,
    })
  },

  setPy: function (e) {
    if (!this.data.isSearchB) {
      return;
    }
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
  },

  //触发全部开始选择
  tStart: function () {
    this.setData({
      hidden: false
    })
    console.log(333)
  },

  //触发结束选择
  tEnd: function () {
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
    console.log(444)
  },


})